home *** CD-ROM | disk | FTP | other *** search
- Algorithm
- Previous: <Interface=>Interface> * Next: <Error Recovery=>ErrorRecov> * Up: <Top=>!Root>
-
- #Wrap on
- {fH2}The Bison Parser Algorithm{f}
-
- As Bison reads tokens, it pushes them onto a stack along with their
- semantic values. The stack is called the {fUnderline}parser stack{f}. Pushing a
- token is traditionally called {fUnderline}shifting{f}.
-
- For example, suppose the infix calculator has read {fEmphasis}1 + 5 \*{f}, with a
- {fEmphasis}3{f} to come. The stack will have four elements, one for each token
- that was shifted.
-
- But the stack does not always have an element for each token read. When
- the last {fStrong}n{f} tokens and groupings shifted match the components of a
- grammar rule, they can be combined according to that rule. This is called
- {fUnderline}reduction{f}. Those tokens and groupings are replaced on the stack by a
- single grouping whose symbol is the result (left hand side) of that rule.
- Running the rule's action is part of the process of reduction, because this
- is what computes the semantic value of the resulting grouping.
-
- For example, if the infix calculator's parser stack contains this:
-
- #Wrap off
- #fCode
- 1 + 5 \* 3
- #f
- #Wrap on
-
- and the next input token is a newline character, then the last three
- elements can be reduced to 15 via the rule:
-
- #Wrap off
- #fCode
- expr: expr '\*' expr;
- #f
- #Wrap on
-
- Then the stack contains just these three elements:
-
- #Wrap off
- #fCode
- 1 + 15
- #f
- #Wrap on
-
- At this point, another reduction can be made, resulting in the single value
- 16. Then the newline token can be shifted.
-
- The parser tries, by shifts and reductions, to reduce the entire input down
- to a single grouping whose symbol is the grammar's start-symbol
- (\*Note <Language and Grammar=>Languagean>: Languages and Context-Free Grammars).
-
- This kind of parser is known in the literature as a bottom-up parser.
-
- #Wrap off
- <Look-Ahead=>LookAhead>: Parser looks one token ahead when deciding what to do.
- <Shift/Reduce=>ShiftReduc>: Conflicts: when either shifting or reduction is valid.
- <Precedence=>Precedencf>: Operator precedence works by resolving conflicts.
- <Contextual Precedence=>Contextual>: When an operator's precedence depends on context.
- <Parser States=>ParserStat>: The parser is a finite-state-machine with stack.
- <Reduce/Reduce=>ReduceRedu>: When two rules are applicable in the same situation.
- <Mystery Conflicts=>MysteryCon>: Reduce\/reduce conflicts that look unjustified.
- <Stack Overflow=>StackOverf>: What happens when stack gets full. How to avoid it.
- #Wrap on
-
-